<?php
class Mapper_Dienst extends DBTable{

	public function __construct(Database $db = null){
		parent::__construct("dienst", $db);
	}


	public function get($dienst_id){
		$query = "
SELECT
	dienst.*,
	dienst_categorie.*,
	IFNULL((
		SELECT
			true
		FROM
			dienst_aangeboden
		WHERE
			dienst_aangeboden.dienst_id = dienst.dienst_id
		LIMIT 1
	), IFNULL((
		SELECT
			true
		FROM
			dienst_gevraagd
		WHERE
			dienst_gevraagd.dienst_id = dienst.dienst_id
		LIMIT 1		
	),false)) AS IsInUse
FROM dienst
LEFT JOIN dienst_categorie USING (dienst_categorie_id)
WHERE
	dienst_id = :dienst_id";
			
		$stmt = $this->prepare($query);
		$stmt->bindParam(":dienst_id", $dienst_id);

		return $stmt->fetchObjectOnce("Dienst");
	}

	public function getAll(){
		$query = "
SELECT
	dienst.*,
	dienst_categorie.*,
	IFNULL((
		SELECT
			true
		FROM
			dienst_aangeboden
		WHERE
			dienst_aangeboden.dienst_id = dienst.dienst_id
		LIMIT 1
	), IFNULL((
		SELECT
			true
		FROM
			dienst_gevraagd
		WHERE
			dienst_gevraagd.dienst_id = dienst.dienst_id
		LIMIT 1		
	),false)) AS IsInUse
FROM dienst
LEFT JOIN dienst_categorie USING (dienst_categorie_id)";
			
		return $this->prepare($query)->fetchObjectAll("Dienst");
			
	}
}